﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows;
using System.Windows.Controls;

namespace FridgeManagerApp
{
    public class ParentWindowViewModel : NotificationObject
    {
        //每个窗口Study都包含独立的窗口
        private Dictionary<EStudyUID, UserControl> gUserControlList = new Dictionary<EStudyUID, UserControl>();
        ParentWindow gParentWindow = null;

        public ParentWindowViewModel(Window pWindow)
        {
            gParentWindow = (ParentWindow)pWindow;

            List<CStudy> studyList = PermissionList.GetInstance().GetStudies();
            foreach (CStudy study in studyList)
            {
                Button button = CreateStudyButton(study);
                gParentWindow.StudyPanel.Children.Add(button);
                gUserControlList.Add(study.StudyUID, null);
            }

            if (studyList != null && studyList.Count > 0)
            {
                List<CSeries> seriesList = PermissionList.GetInstance().GetSerieses(studyList[0].StudyUID);
                foreach (CSeries series in seriesList)
                {
                    Button button = CreateSeriesButton(series);
                    gParentWindow.SeriesPanel.Children.Add(button);
                }
            }

            InitNavigation(studyList[0].StudyUID, ESeriesUID.None);
        }

        private Button CreateStudyButton(CStudy pStudy)
        {
            Button button = new Button();
            button.Uid = ((int)pStudy.StudyUID).ToString();
            button.Margin = new Thickness(5, 5, 5, 0);
            button.Height = 60;
            button.Width = 240;
            button.Content = pStudy.Title;
            button.Click += StudyButtonClick;

            return button;
        }

        private Button CreateSeriesButton(CSeries pSeries)
        {
            Button button = new Button();
            button.Uid = ((int)pSeries.SeriesUID).ToString();
            button.Margin = new Thickness(40, 40, 40, 40);
            button.Height = 240;
            button.Width = 240;
            button.Content = pSeries.Title;
            button.Click += SeriesButtonClick;

            return button;
        }

        private void StudyButtonClick(object sender, RoutedEventArgs e)
        {
            EStudyUID studyUID = (EStudyUID)Convert.ToInt16(((Button)sender).Uid);

            gParentWindow.SeriesPanel.Children.Clear();

            List<CSeries> seriesList = PermissionList.GetInstance().GetSerieses(studyUID);
            foreach (CSeries series in seriesList)
            {
                Button button = CreateSeriesButton(series);
                gParentWindow.SeriesPanel.Children.Add(button);
            }

            gParentWindow.ImagePanel.Children.Clear();
            UserControl userControl = gUserControlList[studyUID];

            if (userControl != null)
            {
                gParentWindow.ImagePanel.Children.Add(userControl);
            }

            if (userControl != null)
            {
                ESeriesUID seriesUID = (ESeriesUID)Convert.ToInt16(userControl.Uid);
                InitNavigation(studyUID, seriesUID);
            }
            else
            {
                InitNavigation(studyUID, ESeriesUID.None);
            }
        }

        private void SeriesButtonClick(object sender, RoutedEventArgs e)
        {
            ESeriesUID seriesUID = (ESeriesUID)Convert.ToInt16(((Button)sender).Uid);

            EStudyUID studyUID = PermissionList.GetInstance().GetStudyUID(seriesUID);

            UserControl imageWindow = CreateUserControl(seriesUID);
            if (imageWindow != null)
            {
                gParentWindow.ImagePanel.Children.Add(imageWindow);
                gUserControlList[studyUID] = imageWindow;
            }

            InitNavigation(studyUID, seriesUID);
        }

        public UserControl CreateUserControl(ESeriesUID pESeriesUID)
        {
            UserControl imageWindow = null;

            switch (pESeriesUID)
            {
                case ESeriesUID.Personnel:
                    imageWindow = new Personnel();
                    PersonnelViewModel personnelViewModel = new PersonnelViewModel(imageWindow, this);
                    imageWindow.DataContext = personnelViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.Personnel).ToString();
                    break;
                case ESeriesUID.PersonnelGroup:
                    imageWindow = new PersonnelGroup();
                    PersonnelGroupViewModel personnelGroupViewModel = new PersonnelGroupViewModel(imageWindow, this);
                    imageWindow.DataContext = personnelGroupViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.PersonnelGroup).ToString();
                    break;
                case ESeriesUID.BD_Area:
                    imageWindow = new Area();
                    AreaViewModel areaViewModel = new AreaViewModel(imageWindow, this);
                    imageWindow.DataContext = areaViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.BD_Area).ToString();
                    break;
                case ESeriesUID.BD_Province:
                    imageWindow = new Province();
                    ProvinceViewModel provinceViewModel = new ProvinceViewModel(imageWindow, this);
                    imageWindow.DataContext = provinceViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.BD_Province).ToString();
                    break;
                case ESeriesUID.BD_City:
                    imageWindow = new City();
                    CityViewModel cityViewModel = new CityViewModel(imageWindow, this);
                    imageWindow.DataContext = cityViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.BD_City).ToString();
                    break;
                case ESeriesUID.BD_District:
                    imageWindow = new District();
                    DistrictViewModel districtViewModel = new DistrictViewModel(imageWindow, this);
                    imageWindow.DataContext = districtViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.BD_District).ToString();
                    break;
                case ESeriesUID.BD_Office:
                    imageWindow = new Office();
                    OfficeViewModel officeViewModel = new OfficeViewModel(imageWindow, this);
                    imageWindow.DataContext = officeViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.BD_Office).ToString();
                    break;
                case ESeriesUID.Permission:
                    imageWindow = new Permission();
                    PermissionViewModel permissionViewModel = new PermissionViewModel(imageWindow, this);
                    imageWindow.DataContext = permissionViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.Permission).ToString();
                    break;
                case ESeriesUID.User:
                    imageWindow = new User();
                    UserViewModel userViewModel = new UserViewModel(imageWindow, this);
                    imageWindow.DataContext = userViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.User).ToString();
                    break;
                case ESeriesUID.Domino:
                    imageWindow = new Domino();
                    DominoViewModel dominoViewModel = new DominoViewModel(imageWindow, this);
                    imageWindow.DataContext = dominoViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.Domino).ToString();
                    break;
                case ESeriesUID.DominoType:
                    imageWindow = new DominoType();
                    DominoTypeViewModel dominoTypeViewModel = new DominoTypeViewModel(imageWindow, this);
                    imageWindow.DataContext = dominoTypeViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.DominoType).ToString();
                    break;
                case ESeriesUID.Device:
                    imageWindow = new Device();
                    DeviceViewModel deviceViewModel = new DeviceViewModel(imageWindow, this);
                    imageWindow.DataContext = deviceViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.Device).ToString();
                    break;
                case ESeriesUID.DeviceType:
                    imageWindow = new DeviceType();
                    DeviceTypeViewModel deviceTypeViewModel = new DeviceTypeViewModel(imageWindow, this);
                    imageWindow.DataContext = deviceTypeViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.DeviceType).ToString();
                    break;
                case ESeriesUID.DeviceCell:
                    imageWindow = new DeviceCell();
                    DeviceCellViewModel deviceCellViewModel = new DeviceCellViewModel(imageWindow, this);
                    imageWindow.DataContext = deviceCellViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.DeviceCell).ToString();
                    break;
                case ESeriesUID.DeviceOfficeNexus:
                    imageWindow = new DeviceOfficeNexus();
                    DeviceOfficeNexusViewModel deviceOfficeNexusViewModel = new DeviceOfficeNexusViewModel(imageWindow, this);
                    imageWindow.DataContext = deviceOfficeNexusViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.DeviceOfficeNexus).ToString();
                    break;
                case ESeriesUID.DeviceStatus:
                    imageWindow = new DeviceStatus();
                    DeviceStatusViewModel deviceStatusViewModel = new DeviceStatusViewModel(imageWindow, this);
                    imageWindow.DataContext = deviceStatusViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.DeviceStatus).ToString();
                    break;
                case ESeriesUID.StockNote:
                    imageWindow = new StockNote();
                    StockNoteViewModel stockNoteViewModel = new StockNoteViewModel(imageWindow, this);
                    imageWindow.DataContext = stockNoteViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.StockNote).ToString();
                    break;
                case ESeriesUID.ReplenishmentNote:
                    imageWindow = new ReplenishmentNote();
                    ReplenishmentNoteViewModel replenishmentNoteViewModel = new ReplenishmentNoteViewModel(imageWindow, this);
                    imageWindow.DataContext = replenishmentNoteViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.ReplenishmentNote).ToString();
                    break;
                case ESeriesUID.SoldNote:
                    imageWindow = new SoldNote();
                    SoldNoteViewModel soldNoteViewModel = new SoldNoteViewModel(imageWindow, this);
                    imageWindow.DataContext = soldNoteViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.SoldNote).ToString();
                    break;
                case ESeriesUID.ExceptionNote:
                    imageWindow = new ExceptionNote();
                    ExceptionNoteViewModel exceptionNoteViewModel = new ExceptionNoteViewModel(imageWindow, this);
                    imageWindow.DataContext = exceptionNoteViewModel;
                    imageWindow.Uid = ((int)ESeriesUID.ExceptionNote).ToString();
                    break;
                default:
                    break;
            }

            return imageWindow;
        }

        public void RemoveUserControl(string pSeriesUID)
        {
            ESeriesUID seriesUID = (ESeriesUID)Convert.ToInt16(pSeriesUID);
            gParentWindow.ImagePanel.Children.Clear();
            gUserControlList[PermissionList.GetInstance().GetStudyUID(seriesUID)] = null;

            InitNavigation(PermissionList.GetInstance().GetStudyUID(seriesUID), ESeriesUID.None);

        }

        public void InitNavigation(EStudyUID pEStudyUID, ESeriesUID pESeriesUID)
        {
            if (pEStudyUID == EStudyUID.None)
            {
                gParentWindow.StudyName.Text = "";
            }
            else
            {
                gParentWindow.StudyName.Text = PermissionList.GetInstance().GetStudy(pEStudyUID).Title;
            }
            gParentWindow.StudyName.Uid = ((int)pEStudyUID).ToString();

            if (pESeriesUID == ESeriesUID.None)
            {
                gParentWindow.SeriesName.Content = "";
            }
            else
            {
                gParentWindow.SeriesName.Content = PermissionList.GetInstance().GetSeries(pESeriesUID).Title;
            }
            gParentWindow.SeriesName.Uid = ((int)pESeriesUID).ToString();
        }

        protected DelegateCommand<object> naviStudyCommand;
        public DelegateCommand<object> NaviStudyCommand
        {
            get
            {
                return naviStudyCommand ?? (naviStudyCommand = new DelegateCommand<object>(ExecuteNaviStudyCommand, CanExecuteNaviStudyCommand));
            }
        }

        private void ExecuteNaviStudyCommand(object obj)
        {
            RemoveUserControl(gParentWindow.SeriesName.Uid);
        }

        private bool CanExecuteNaviStudyCommand(object arg)
        {
            return true;
        }
    }
}
